function ccR_Paladin_LayOnHands()
	if ccrPlayerHasSpell(LAY_ON_HANDS)
	and ccRS.Paladin_UseLayOnHands
	and ccrPlayerHealth() <= ccRS.Paladin_LayOnHandsHP
	and not ccrPlayerHasDebuff(FORBEARANCE)
	and SpellIsReady(LAY_ON_HANDS)
	then
		ccrCast(LAY_ON_HANDS, "player")
		return
	end
end

function ccR_Paladin_DivineShield()
	if ccrPlayerHasSpell(DIVINE_SHIELD)
	and ccRS.Paladin_UseDivineShield
	and ccrPlayerHealth() <= ccRS.Paladin_DivineShieldHP
	and not ccrPlayerHasDebuff(FORBEARANCE)
	and SpellIsReady(DIVINE_SHIELD)
	then
		ccrCast(DIVINE_SHIELD, "player")
		return
	end
end

function ccR_Paladin_Cleanse()
	if ccrPlayerHasSpell(4987)
	and	ccRS.Paladin_UseCleanse
	and SpellIsReady(4987)
	then
		-- Scan for a debuff for Poison/Disease
		local i = 1
		for i = 1, 40 do
			local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId  = UnitDebuff("player", i) 
			-- No debuff found, Exit the check
			if name == nil then
				break
			else
				-- Found a debuff, Check if Poison or Disease
				if debuffType == "Poison"
				or debuffType == "Disease"
				then
					-- Compare how long this debuff will last against settings in the gui
					if (expirationTime-GetTime()) >= ccRS.Paladin_CleanseDuration then
						-- Debuff is exact or more on duration, Use cleanse on self.
						ccrCast(4987, "player")
						return
					end
				end
			end
		end
		return
	end
end

function ccR_Paladin_DivineProtection()
	if ccrPlayerHasSpell(DIVINE_PROTECTION)
	and ccRS.Paladin_UseDivineProtection
	and ccrPlayerHealth() <= ccRS.Paladin_DivineProtectionHP
	and SpellIsReady(DIVINE_PROTECTION)
	then
		ccrCast(DIVINE_PROTECTION)
		return
	end
	return
end

function ccR_Paladin_Rebuke()
	if ccrPlayerHasSpell(REBUKE)
	and ccRS.Paladin_UseRebuke
	and SpellIsReady(REBUKE)
	and ccrTargetIsCastingOrChanneling()
	and ccrTargetIsInteruptable()
	then
		-- Immedietly interupt chaneling
		if ccrTargetIsChanneling then
			ccrCastAttack(REBUKE)
			return
		end
		-- Interupt casting with 1sec or less left
		if ccrTargetIsCasting()
		and ccrTargetHasLeftOnCast(1)
		then
			ccrCastAttack(REBUKE)
			return
		end
		return
	end
	return
end

function ccR_Paladin_HandOfProtection()
	if ccrPlayerHasSpell(HAND_OF_PROTECTION)
	and ccRS.Paladin_UseHandOfProtection
	and ccrPlayerHealth() <= ccRS.Paladin_HandOfProtectionHP
	and not ccrPlayerHasDebuff(FORBEARANCE)
	and SpellIsReady(HAND_OF_PROTECTION)
	then
		ccrCast(HAND_OF_PROTECTION, "player")
		return
	end
end

function ccR_Paladin_HandOfFreedom()
	if ccrPlayerHasSpell(HAND_OF_FREEDOM)
	and ccRS.Paladin_UseHandOfFreedom
	and SpellIsReady(HAND_OF_FREEDOM)
	and not ccrPlayerHasBuff(HAND_OF_FREEDOM)
	then
		local eventIndex = C_LossOfControl.GetNumEvents()
		while (eventIndex > 0) do
			local _, _, text = C_LossOfControl.GetEventInfo(eventIndex)
			if text == LOSS_OF_CONTROL_DISPLAY_ROOT 
			or text == LOSS_OF_CONTROL_DISPLAY_SNARE
			then
				ccrCast(HAND_OF_FREEDOM, "player")
				return
			end
			eventIndex = eventIndex - 1
		end
	end
	return
end

function ccR_Paladin_Avenging_Wrath()
	-- Avenging Wrath
	if ccrPlayerHasSpell(AVENGING_WRATH)
	and ccrPlayerIsInCombat()
	and ccrTargetIsWithinMeleeRange()
	and SpellIsReady(AVENGING_WRATH)
	then
		ccrCast(AVENGING_WRATH)
		return
	end
	return
end

function ccR_Paladin_Guardian_of_Ancient_Kings()
	-- Guardian of Ancient Kings
	if ccrPlayerHasSpell(GUARDIAN_OF_ANCIENT_KINGS)
	and ccrPlayerIsInCombat()
	and ccrTargetIsWithinMeleeRange()
	and (ccrTargetIsBoss() or ccrTargetIsRare() or ccrTargetIsEnemyPlayer())
	and SpellIsReady(GUARDIAN_OF_ANCIENT_KINGS)
	then
		if ccrPlayerSpec() == 2
		and ccrPlayerHealth() <= 50
		then
			ccrCast(GUARDIAN_OF_ANCIENT_KINGS)
			return
		end
		if ccrPlayerSpec() == 1
		or ccrPlayerSpec() == 3
		then
			ccrCast(GUARDIAN_OF_ANCIENT_KINGS)
			return
		end
		return
	end
	return
end

function ccR_Paladin_Blessing_of_Kings()
	-- Blessings of Kings if missing
	if ccrPlayerHasSpell(BLESSING_OF_KINGS)
	and not ccrPlayerHasBuff(BLESSING_OF_KINGS)
	and not ccrPlayerHasBuff(BLESSING_OF_MIGHT)
	and not ccrPlayerHasBuff(1126) -- Mark of the Wild
	and not ccrPlayerHasBuff(115921) -- Legacy of the Emperor
	and not ccrPlayerHasBuff(90363) -- Embrace of the Shale Spider 
	then
		ccrCast(BLESSING_OF_KINGS)
		return
	end
end

function ccR_Paladin_Hammer_Of_Wrath()
	-- Hammer of Wrath
	if ccrPlayerHasSpell(HAMMER_OF_WRATH)
	and SpellCanAttack(HAMMER_OF_WRATH)
	then
		ccrCastAttack(HAMMER_OF_WRATH)
		return
	end
	return
end

function ccR_Paladin_Exorcism()
	-- Exorcism
	if ccrPlayerHasSpell(EXORCISM)
	and SpellCanAttack(EXORCISM)
	then
		ccrCastAttack(EXORCISM)
		return
	end
	return
end

function ccR_Paladin_Inquisition()
	-- Inquisition Buff
	if ccrPlayerHasSpell(INQUISITION)
	and not ccrPlayerHasBuff(INQUISITION)
	and (ccrHolyPower() > 0 or ccrPlayerHasBuff(DIVINE_PURPOSE))
	and SpellIsReady(INQUISITION)
	then
		ccrCast(INQUISITION)
		return
	end
	return
end

function ccR_Paladin_Crusader_Strike()
	-- Crusader Strike
	if ccrPlayerHasSpell(CRUSADER_STRIKE)
	and SpellCanAttack(CRUSADER_STRIKE)
	then
		ccrCastAttack(CRUSADER_STRIKE) 
		return
	end
	return
end

function ccR_Paladin_Templars_Verdict()
	-- Templar's Verdict
	if ccrPlayerHasSpell(TEMPLARS_VERDICT)
	and SpellCanAttack(TEMPLARS_VERDICT)
	and (ccrHolyPower() >= 3 or ccrPlayerHasBuff(DIVINE_PURPOSE))
	then
		ccrCastAttack(TEMPLARS_VERDICT)
		return
	end
	return
end

function ccR_Paladin_Judgment()
	-- Judgment
	if ccrPlayerHasSpell(JUDGMENT)
	and SpellCanAttack(JUDGMENT)
	then
		ccrCastAttack(JUDGMENT)
		return
	end	
	return
end

function ccR_Paladin_Hammer_of_the_Righteous()
	-- Hammer of the Righteous
	if ccrPlayerHasSpell(HAMMER_OF_THE_RIGHTEOUS)
	and SpellCanAttack(HAMMER_OF_THE_RIGHTEOUS)
	and not ccrPlayerHasBuff(DIVINE_PURPOSE)
	then
		ccrCastAttack(HAMMER_OF_THE_RIGHTEOUS)
		return
	end
	return
end

function ccR_Paladin_Divine_Storm()
	-- Divine Storm
	if ccrPlayerHasSpell(DIVINE_STORM)
	and SpellIsReady(DIVINE_STORM)
	and (ccrHolyPower() >= 3 or ccrPlayerHasBuff(DIVINE_PURPOSE))
	and Target
	and Target:Exists() 
	and Target:GetDistance(Player) <= 12
	then
		ccrCast(DIVINE_STORM)
		return
	end
	return
end

--
-- PROTECTION
-- 

function ccR_Paladin_Avengers_Shield()
	if ccrPlayerHasSpell(AVENGERS_SHIELD)
	and ccrSpellCanAttack(AVENGERS_SHIELD)
	and ccrPlayerIsFacingTarget()
	then	
		ccrCastAttack(AVENGERS_SHIELD)
		return
	end
	return
end
function ccR_Paladin_Shield_of_the_Righteous()
	if ccrPlayerHasSpell(SHIELD_OF_THE_RIGHTEOUS)
	and ccrSpellCanAttack(SHIELD_OF_THE_RIGHTEOUS)
	and (ccrHolyPower() >= 3 or ccrPlayerHasBuff(DIVINE_PURPOSE))
	then	
		ccrCastAttack(SHIELD_OF_THE_RIGHTEOUS)
		return
	end
	return
end

function ccR_Paladin_Consecration()
	if ccrPlayerHasSpell(CONSECRATION)
	and SpellIsReady(CONSECRATION)
	and ccrTargetIsWithinMeleeRange()
	then
		ccrCast(CONSECRATION)
		return
	end
end

function ccR_Paladin_Holy_Wrath()
	if ccrPlayerHasSpell(HOLY_WRATH)
	and ccrSpellIsReady(HOLY_WRATH)
	and ccrTargetIsWithinMeleeRange()
	then
		ccrCast(HOLY_WRATH)
		return
	end
	return
end

function ccR_Paladin_Word_of_Glory()
	if ccrPlayerHasSpell(WORD_OF_GLORY)
	and (ccrHolyPower() >= 3 or ccrPlayerHasBuff(DIVINE_PURPOSE))
	and ccrPlayerHealth() <= 80
	and ccrSpellIsReady(WORD_OF_GLORY)
	then
		ccrCast(WORD_OF_GLORY)
		return
	end
	return
end

function ccR_Paladin_Shield_of_the_Righteous_and_Word_of_Glory()
	if ccrPlayerHasSpell(SHIELD_OF_THE_RIGHTEOUS) then
		if ccrPlayerHealth() >= 60 then
			ccR_Paladin_Shield_of_the_Righteous()
		else
			ccR_Paladin_Word_of_Glory()
		end
	else
		ccR_Paladin_Word_of_Glory()
	end
	return
end